package com.pur.zsms.unit.dao.impl;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.Query;

import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Repository;

import com.pur.zsms.common.dao.GenericDao;
import com.pur.zsms.common.dao.JpaTemplate;
import com.pur.zsms.common.exception.GenericException;
import com.pur.zsms.common.support.PageController;
import com.pur.zsms.common.support.PaginationSupport;
import com.pur.zsms.foodmanage.food.dao.IFoodDao;
import com.pur.zsms.foodmanage.food.po.FoodMaterPo;
import com.pur.zsms.foodmanage.food.po.FoodPo;
import com.pur.zsms.foodmanage.food.vo.FoodMaterVo;
import com.pur.zsms.foodmanage.food.vo.FoodVo;
import com.pur.zsms.matermanage.mater.po.MaterPo;
import com.pur.zsms.pmsnmanage.menu.po.MenuPo;
import com.pur.zsms.pmsnmanage.role.vo.RoleVo;
import com.pur.zsms.pmsnmanage.user.dao.IUserDao;
import com.pur.zsms.pmsnmanage.user.po.UnitMenuPo;
import com.pur.zsms.pmsnmanage.user.po.UnitPo;
import com.pur.zsms.pmsnmanage.user.po.UserPo;
import com.pur.zsms.pmsnmanage.user.po.UserRolePo;
import com.pur.zsms.pmsnmanage.user.vo.UserVo;
import com.pur.zsms.supplier.po.SupplierPo;
import com.pur.zsms.systemsetting.datadictionary.po.CodeType;
import com.pur.zsms.unit.dao.IUnitDao;
import com.pur.zsms.unit.vo.UnitCommentVo;
import com.pur.zsms.unit.vo.UnitMenuVo;
import com.pur.zsms.unit.vo.UnitVo;

@Repository("unitDao")
public class UnitDaoImpl extends GenericDao implements IUnitDao {

	public PaginationSupport listUnitComment(PageController pageController, UnitCommentVo unitCommentVo)
	        throws GenericException {
		StringBuffer ql = new StringBuffer();
		ql.append("SELECT a.`cid`,a.`unit_id`,b.`unit_name`,a.`comment_state`,a.`comment_text`,a.`comment_date` FROM `t_unit_comment` a,t_unit b WHERE  a.`unit_id`=b.`unit_id`  ");

		if (unitCommentVo.getCommentState() != 0) {
			ql.append(" and a.`comment_state`='" + unitCommentVo.getCommentState() + "' ");
		}
		if (unitCommentVo.getCommentDateStart() != null && !unitCommentVo.getCommentDateStart().equals("")) {
			ql.append(" and a.`comment_date` >= '" + unitCommentVo.getCommentDateStart() + "' ");
		}
		if (unitCommentVo.getCommentDateEnd() != null && !unitCommentVo.getCommentDateEnd().equals("")) {
			ql.append(" and a.`comment_date` <= '" + unitCommentVo.getCommentDateEnd() + "' ");
		}
		if (unitCommentVo.getUnitName() != null && !unitCommentVo.getUnitName().equals("")) {
			ql.append(" and b.`unit_name` like '%" + unitCommentVo.getUnitName() + "%' ");
		}

		return this.findByNativeQuery(ql.toString(), pageController);
	}

	public PaginationSupport listUnit(PageController pageController, UnitVo unitVo) throws GenericException {
		StringBuffer ql = new StringBuffer();
		ql.append("SELECT u.unit_id,u.unit_name,u.unit_code FROM t_unit u ");

		return this.findByNativeQuery(ql.toString(), pageController);
	}

	public PaginationSupport listUnitMenu(PageController pageController, UnitMenuVo unitMenuVo)
	        throws GenericException {
		StringBuffer ql = new StringBuffer();
		ql.append("SELECT a.menu_id, b.unit_id, b.unit_name, a.menu_date, a.menu_text FROM t_unit_menu a, t_unit b WHERE a.unit_code = b.unit_code   ");

		if (unitMenuVo.getMenuDateStart() != null && !unitMenuVo.getMenuDateStart().equals("")) {
			ql.append(" and a.`menu_date` >= '" + unitMenuVo.getMenuDateStart() + "' ");
		}
		if (unitMenuVo.getMenuDateEnd() != null && !unitMenuVo.getMenuDateEnd().equals("")) {
			ql.append(" and a.`menu_date` <= '" + unitMenuVo.getMenuDateEnd() + "' ");
		}
		if (unitMenuVo.getUnitName() != null && !unitMenuVo.getUnitName().equals("")) {
			ql.append(" and b.`unit_name` like '%" + unitMenuVo.getUnitName() + "%' ");
		}

		return this.findByNativeQuery(ql.toString(), pageController);
	}

	public List<UnitPo> getAllUnitPo() throws GenericException {
		JpaTemplate template = this.getTemplate();
		StringBuffer ql = new StringBuffer();
		ql.append(" select o from UnitPo o ");
		List<UnitPo> list = template.find(ql.toString(), null);
		return list;
	}

	public String getUnitPoByUnitId(int unitId) throws GenericException {
		JpaTemplate template = this.getTemplate();
		StringBuffer ql = new StringBuffer();
		ql.append(" select o from UnitPo o where o.unitId =" + unitId);
		List<UnitPo> list = template.find(ql.toString(), null);
		return list.get(0).getUnitCode();
	}

	public int getUnitIdByUnitCode(String unitCode) throws GenericException {
		JpaTemplate template = this.getTemplate();
		StringBuffer ql = new StringBuffer();
		ql.append(" select o from UnitPo o where o.unitCode ='" + unitCode + "'");
		List<UnitPo> list = template.find(ql.toString(), null);
		if (list != null && list.size() > 0) {
			return list.get(0).getUnitId();
		} else {
			return 0;
		}
	}

	public List<UnitMenuPo> getUnitMenuPo(UnitMenuVo c) throws GenericException {
		JpaTemplate template = this.getTemplate();
		StringBuffer ql = new StringBuffer();
		ql.append(" select o from UnitMenuPo o  where o.unitCode = '" + c.getUnitCode()
		        + "' and o.menuDate='" + c.getMenuDate() + "'");
		List<UnitMenuPo> list = template.find(ql.toString(), null);
		return list;
	}

	public List<UnitMenuPo> getUnitMenuPoByVo(UnitMenuVo c) throws GenericException {
		JpaTemplate template = this.getTemplate();
		StringBuffer ql = new StringBuffer();
		ql.append(" select o from UnitMenuPo o  where 1=1 ");
		if (c.getMenuId() != 0) {
			ql.append(" and o.menuId =" + c.getMenuId());

		}
		List<UnitMenuPo> list = template.find(ql.toString(), null);
		return list;
	}

	public void addUnitMenuPo(UnitMenuPo c) throws GenericException {
		this.merge(c);
	}
}
